//==============================================================================
// Copyright (c) 2008-2013 Niall Ryan. All Rights Reserved.
//==============================================================================

#ifndef HYDRA_LOCKS_TTASLOCK_H
#define HYDRA_LOCKS_TTASLOCK_H 1

#include "hydra/atomic/Atomic.h"
#include "hydra/backoff/Backoff.h"

namespace Hydra
{
	class TTASLock
	{
	public:
		TTASLock()
		{
			m_flag = false;
		}
		void lock()
		{
			Backoff backoff;
			while( true )
			{
				while( m_flag )	{ backoff.wait(); }
				if( m_flag.getAndSet(true) )
					backoff.wait();
				else
					return;
			}
		}
		void unlock()
		{
			m_flag = false;
		}
		bool isLocked() const
		{
			return m_flag;
		}
	private:
		Atomic<bool> m_flag;
	};
}

#endif
